package com.gewu.pm.ui.fragment.terminal;

import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.bean.TerminalContactBean;
import com.gewu.pm.ui.activity.terminal.ContactDetailsActivity;
import com.gewu.pm.ui.adapter.terminal.TerminalDetailContactAdapter;
import com.gewu.pm.ui.dialog.TipsTwoDialog;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 终端详情 - 联系人 Fragment
 * <p>
 * 功能：
 * 1. 显示联系人列表
 * 2. 右下角浮动按钮（icon_big_add.png）添加联系人
 * 3. 右上角三个按钮（聊天、邮件、电话）
 * 4. 底部删除和编辑按钮
 */
public final class ContactFragment extends AppFragment<AppActivity>
        implements BaseAdapter.OnItemClickListener {

    private RecyclerView mRecyclerView;
    private TerminalDetailContactAdapter mAdapter;
    private ImageView mFabAddButton;  // 右下角浮动按钮

    public static ContactFragment newInstance() {
        return new ContactFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.terminal_contact_fragment;
    }

    @Override
    protected void initView() {
        mRecyclerView = findViewById(R.id.rv_contact_list);
        mFabAddButton = findViewById(R.id.fab_add_contact);

        // 设置 RecyclerView
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new TerminalDetailContactAdapter(getContext());
        mAdapter.setOnContactActionListener(new TerminalDetailContactAdapter.OnContactActionListener() {
            @Override
            public void onChat(int position) {
                toast("聊天");
                // TODO: 打开聊天
            }

            @Override
            public void onEmail(int position) {
                toast("发送邮件");
                // TODO: 打开邮件
            }

            @Override
            public void onPhone(int position) {
                toast("拨打电话");
                // TODO: 拨打电话
            }

            @Override
            public void onEdit(TerminalContactBean contact, int position) {
                toast("编辑联系人：" + contact.getName());
                // TODO: 打开编辑联系人页面
            }

            @Override
            public void onDelete(int position) {
                // TODO: 删除联系人
                new TipsTwoDialog.Builder(getContext()).setTitle("您确定删除联系人吗").setListener(dialog -> {
                    mAdapter.removeItem(position);
                }).show();

            }
        });

        // 必须在 setAdapter 之前设置监听器
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

        // 设置浮动按钮点击事件
        mFabAddButton.setOnClickListener(v -> {
            toast("新增联系人");
            // TODO: 打开新增联系人页面
        });
    }

    @Override
    protected void initData() {
        // TODO: 加载联系人数据
        List<TerminalContactBean> testData = new ArrayList<>();
        TerminalContactBean contact1 = new TerminalContactBean();
        contact1.setName("刘宏远");
        contact1.setPhone("18056787869");
        contact1.setPrimary(true);
        testData.add(contact1);

        TerminalContactBean contact2 = new TerminalContactBean();
        contact2.setName("联系人二");
        contact2.setPhone("18056787870");
        contact2.setPrimary(false);
        testData.add(contact2);

        mAdapter.setData(testData);
    }

    @Override
    public void onItemClick(androidx.recyclerview.widget.RecyclerView recyclerView, View itemView, int position) {
        TerminalContactBean item = mAdapter.getItem(position);
        // TODO: 打开联系人详情
        ContactDetailsActivity.start(getAttachActivity(), item.getName());
    }
}

